package com.bawei.module_home.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.lib_image.GlideApp
import com.bawei.module_home.R
import com.bawei.lib_common.entity.SimpleVideoEntityItem

class SimpleVideoAdapter(
    var onItemClick: ((position: Int) -> Unit)? = null,
    var onItemCommentClick: ((position: Int) -> Unit)? = null
): RecyclerView.Adapter<SimpleVideoAdapter.SimpleVideoHolder>() {
     val simpleVideoList= mutableListOf<SimpleVideoEntityItem>()
    fun setSimpleVideoData(data:List<SimpleVideoEntityItem>){
        simpleVideoList.addAll(data)
        notifyDataSetChanged()
    }
    fun clear(){
        simpleVideoList.clear()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleVideoHolder {
        return SimpleVideoHolder(View.inflate(parent.context,R.layout.item_simple_video,null),onItemClick,onItemCommentClick)
    }

    override fun getItemCount(): Int {
        return simpleVideoList.size
    }

    override fun onBindViewHolder(holder: SimpleVideoHolder, position: Int) {
        val item = simpleVideoList[position]
        GlideApp.with(BaseApplication.instance).load(item.videomainimag).placeholder(R.drawable.error_image).into(holder.image)
        holder.desc.text = item.description
        GlideApp.with(BaseApplication.instance).load(item.avatar_url).circleCrop().into(holder.icon)
        holder.authorName.text = item.name
    }
    class SimpleVideoHolder(view: View,var onItemClick: ((position: Int) -> Unit)? = null,var onItemCommentClick: ((position: Int) -> Unit)? = null) : RecyclerView.ViewHolder(view) {
        val image = view.findViewById<ImageView>(R.id.simple_video_image)
        val icon = view.findViewById<ImageView>(R.id.simple_video_icon)
        val comment = view.findViewById<ImageView>(R.id.simple_video_follow)
        val desc = view.findViewById<TextView>(R.id.simple_video_desc)
        val authorName = view.findViewById<TextView>(R.id.simple_video_author_name)
        init {
            image.setOnClickListener {
                onItemClick?.invoke(position)
            }
            comment.setOnClickListener {
                onItemCommentClick?.invoke(position)
            }
        }
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = 0,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.run {
            setItemViewCacheSize(12)//设置屏幕外ViewHolder缓存数量
            setHasFixedSize(true)//减少固定大小的子组件反复测量
            setRecycledViewPool(RecyclerView.RecycledViewPool().apply { setMaxRecycledViews(0, 10) })
            setViewCacheExtension(VideoItemCache(recyclerView))
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE){
                        GlideApp.with(BaseApplication.instance).resumeRequests()
                    }else{
                        GlideApp.with(BaseApplication.instance).pauseRequests()
                    }
                }
            })
        }
    }
}